import Vue from 'vue'
import VueRouter from 'vue-router'
import NoAuthorization from '../components/NoAuthorization.vue' // 无权限提示组件,没有必要设定成懒加载形式

import store from '../vuex'

Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'hash',
  // 将公用路由注册到路由表中
  routes: [
    { path: '/', component: () => import('../components/A.vue') },
    { path: '/b', component: () => import('../components/B.vue') },
    // 对于用户自己手动的用输入链接访问路由表中不存在的路由组件,那么就直接提示无权访问.
    { path: '*', component: NoAuthorization }

    // { path: '/c', component: () => import('../components/C.vue') }, // C 角色的专属路由
    // { path: '/d', component: () => import('../components/D.vue') } // D 角色的专属路由
  ]
})

// const url = '/static/C.json'
const url = '/static/D.json'
// 动态路由缓存
store.dispatch('setUserRoutes', url).then(routes => {
  // 动态路由加载,包括缓存功能
  routes.map(({ path, name, component }) => ({
    path,
    name,
    component: () => import(`../components/${component}.vue`)
  })).forEach(route => {
    router.addRoute(route)
  })
  console.log(router.getRoutes());
})




export default router

